package com.session;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class WelcomeServlet
 */
@WebServlet("/WelcomeServlet")
public class WelcomeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public WelcomeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		PrintWriter pw=response.getWriter();
		HttpSession session=request.getSession(false);
		
	 Cookie cookie[]=	 request.getCookies();
		if(cookie!=null){
	 for (Cookie cookie2 : cookie) {
		 
		 System.out.println(cookie2.getName());
		 System.out.println(cookie2.getValue());
	}
		 if(session==null){
			 System.out.println("redirectiing..");
			 response.sendRedirect("login.html");
			 return;
		 }}
		 
		 System.out.println(session);
		  if(session!=null) {
	String userName=(String)session.getAttribute("userName");
	ActiveUserDetails activeUserDetails=(ActiveUserDetails)session.getAttribute("activeUser");
	/*	if(null==userName) {
			response.sendRedirect("login.html");
		}*/
	response.setContentType("text/html"); //MimE TYPE 
		pw.write("Welcome "+userName);
		pw.write("Welcome "+activeUserDetails.getUseId());
		pw.write("<html> <body>< input type='text'> </body></html>");
		
		  }else {
			  response.sendRedirect("login.html");
		  }
	}

}
